﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using LibSars;

namespace SarsClient
{
    class MessageSender
    {
        ConnectionManager cmgr;	

        public MessageSender(ConnectionManager p_cmgr)
        {
            cmgr = p_cmgr;
            //TODO: maybe do some clever things
        }

        public void Send(String text)
        {
            //TODO: Get this from somewhere else
            //RoomList roomList = new RoomList();

            //roomList.getRoomMembers();

            foreach (Peer member in cmgr.GetAllPeers())
            {
                new Thread(delegate()
                {
                    SarsPacket message = new SarsPacket();
                    SarsPacketMessageSAITHIS textMessage = new SarsPacketMessageSAITHIS();
                    message.Message = textMessage;
                    textMessage.Text = text;
                    message.SenderId = cmgr.OwnId;
                    message.RecipientId = member.Id;
                    cmgr.SendPacket(message);
                    Logging.Logger.Debug("bla");
                    
                    //TODO: Send 'text'
                    /*
                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(new IPEndPoint(p_ipAddr, p_port));
                    socket.Send(message.GenerateBytes());
                    */

                   
                }).Start();
            }
        }
    }
}
